package com.itcaius.weblog.admin.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.itcaius.weblog.admin.service.AdminStatisticsService;
import com.itcaius.weblog.common.domain.dos.ArticleCategoryRelDO;
import com.itcaius.weblog.common.domain.dos.ArticleTagRelDO;
import com.itcaius.weblog.common.domain.dos.CategoryDO;
import com.itcaius.weblog.common.domain.dos.TagDO;
import com.itcaius.weblog.common.domain.mapper.ArticleCategoryRelMapper;
import com.itcaius.weblog.common.domain.mapper.ArticleTagRelMapper;
import com.itcaius.weblog.common.domain.mapper.CategoryMapper;
import com.itcaius.weblog.common.domain.mapper.TagMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Caius
 * @description
 * @since Created in 2025-06-15
 */
@Service
@Slf4j
public class AdminStatisticsServiceImpl implements AdminStatisticsService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ArticleCategoryRelMapper articleCategoryRelMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleTagRelMapper articleTagRelMapper;

    @Override
    public void statisticsCategoryArticleTotal() {
        // 查询所有分类
        List<CategoryDO> categoryDOS = categoryMapper.selectList(Wrappers.emptyWrapper());
        // 查询所有文章分类关联关系
        List<ArticleCategoryRelDO> articleCategoryRelDOS = articleCategoryRelMapper.selectList(Wrappers.emptyWrapper());

        // 分类 ID 和文章分类关联关系的映射关系
        Map<Long, List<ArticleCategoryRelDO>> categoryIdAndArticleCategoryRelDOMap = Maps.newHashMap();

        // 按照所属分类进行分组
        if (!CollectionUtils.isEmpty(articleCategoryRelDOS)) {
            categoryIdAndArticleCategoryRelDOMap = articleCategoryRelDOS.stream()
                    .collect(Collectors.groupingBy(ArticleCategoryRelDO::getCategoryId));
        }

        if (!CollectionUtils.isEmpty(categoryDOS)) {
            for (CategoryDO categoryDO : categoryDOS) {
                Long categoryId = categoryDO.getId();

                // 获取该分类下面的文章分类关联关系
                List<ArticleCategoryRelDO> articleCategoryRelDOList = categoryIdAndArticleCategoryRelDOMap.get(categoryId);

                int count = CollectionUtils.isEmpty(articleCategoryRelDOList) ? 0 : articleCategoryRelDOList.size();

                // 更新文章分类关联关系
                CategoryDO categoryDO1 = CategoryDO.builder()
                        .id(categoryId)
                        .articlesTotal(count)
                        .build();
                categoryMapper.updateById(categoryDO1);
            }
        }
    }

    /**
     * 统计标签下的文章数量
     */
    @Override
    public void statisticsTagArticleTotal() {
        // 查询所有标签
        List<TagDO> tagDOS = tagMapper.selectList(Wrappers.emptyWrapper());

        // 查询所有文章标签关联关系
        List<ArticleTagRelDO> articleTagRelDOS = articleTagRelMapper.selectList(Wrappers.emptyWrapper());

        // 按照所属标签进行分组
        Map<Long, List<ArticleTagRelDO>> tagIdAndArticleTagRelDOMap = Maps.newHashMap();
        if (!CollectionUtils.isEmpty(articleTagRelDOS)) {
            tagIdAndArticleTagRelDOMap = articleTagRelDOS.stream()
                    .collect(Collectors.groupingBy(ArticleTagRelDO::getTagId));
        }

        if (!CollectionUtils.isEmpty(tagDOS)) {
            for (TagDO tagDO : tagDOS) {
                Long tagId = tagDO.getId();

                List<ArticleTagRelDO> articleTagRelDOList = tagIdAndArticleTagRelDOMap.get(tagId);

                int articlesTotal = CollectionUtils.isEmpty(articleTagRelDOList) ? 0 : articleTagRelDOList.size();

                TagDO updatedTagDO = TagDO.builder()
                        .id(tagId)
                        .articlesTotal(articlesTotal)
                        .build();
                tagMapper.updateById(updatedTagDO);
            }
        }
    }
}
